package com.awesumgames.awesum;

import java.util.ArrayDeque;

import com.awesumgames.awesum.AwesumGlobal.Awesum;

//import com.awesumgames.awesum.SceneCollection.RunningScene;

public class SceneCollection implements Runnable {
	private ArrayDeque<RunningScene> scenes;
	public enum SceneMode { REPLACE, INTERRUPT, CONCURRENT }

	public SceneCollection() {
		scenes = new ArrayDeque<RunningScene>();
	}

	public Scene add(Scene s) {
		return add(s, SceneMode.REPLACE);
	}
	public Scene add(Scene s, SceneMode m) {
		RunningScene rs = null;
		int lastLevel = scenes.size() == 0 ? 0 : scenes.getLast().level;
		
		switch (m) {
			case REPLACE:
				pop(lastLevel);
				
				//TODO: clear the scene's views off of the glsurfaceview or activity
				//Game.activity.
				
				rs = new RunningScene(s, lastLevel);
				break;
			case INTERRUPT:
				rs = new RunningScene(s, lastLevel + 1);
				break;
			case CONCURRENT:
				rs = new RunningScene(s, lastLevel);
				break;
			default:
				//TODO: exception
		}
		
		scenes.add(rs);
		Game.scene = s;
		s.load();
		return s;
	}

	public Scene pop(int location) {
		//pop only the newest scene added
		if (scenes.size() > 0) {
			scenes.removeLast();
			return scenes.size() > 0 ? scenes.getLast().scene : null;
		}
		return null;
	}
	public Scene popLevel() {
		//Pop all scenes running concurrently
		//RunningScene last = scenes.getLast();
		//int lastLevel = last.level;
		
		
		int last = scenes.getLast().level;
		while (scenes.getLast().level == last)
			scenes.pop();
		
		return scenes.getLast().scene;
	}

	public int size() {
		return scenes.size();
	}
	
	public void load() {
		/*for (Iterator<RunningScene> i = scenes.iterator(); i.hasNext();) {
			
		}*/
		/*for (RunningScene rs : scenes) {
			rs.scene.load();
		}*/
		Awesum.debug("scenecol::load, set these up to only run the top level based on RS.level");
	}
	public void draw() {
	}
	public void step() {
	}
	
	
	private class RunningScene {
		public Scene scene;
		public int level;
		public RunningScene(Scene s, int lvl) {
			scene = s; level = lvl;
		}
	}
}
